

import 'package:baselib/utils/keyboard_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';


class _PageFullWidget extends StatefulWidget {
  bool? isAutomaticKeepAlive;
  Widget child;
  _PageFullWidget({super.key,this.isAutomaticKeepAlive,required this.child});

  @override
  State<_PageFullWidget> createState() => _PageFullWidgetState();
}

class _PageFullWidgetState extends State<_PageFullWidget> with AutomaticKeepAliveClientMixin{
  @override
  Widget build(BuildContext context) {

    return KeyBoardUtil.KeyBoardOnTouchDownWidget(context, child: widget.child);
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => (widget.isAutomaticKeepAlive??false);
}


/**
 * 普通页面和 GetWidget 一起使用，需要注册路由并绑定viewmodel
 * 例子：
 * 1、页面：
 class HomePage extends GetWidget{
    @override
    Widget build(BuildContext context) {
      return PageRouteBuild<HomeViewModel>(builder: (viewModel){
        return Scaffold(
          ...
        );
      });
    }
}
 * 2、路由注册：
    GetPage(
    name: RouteConfig.homePage,
    page: () => HomePage(),
    binding:
    BindingsBuilder(() => [Get.lazyPut(() => HomeViewModel())])),
 */
Widget PageRouteBuild<T extends GetxController>({Key? key,required Function(T) builder}){
  return GetBuilder(
      builder: (T controller)=>builder(controller)
  );
}
/**
 * 用于TabBar+TabBarView的情况，不需要注册路由
 例如:
子页面：
class IndexPage extends GetWidget {
    @override
    Widget build(BuildContext context) {
      return PageTabBuild<IndexViewModel>(
        initController: Get.put(IndexViewModel(), tag: 'IndexViewModel'),
        tag: 'IndexViewModel',
        builder: (viewModel) {
        return PageWidgetManager.buildPage(
          context: context,
          backgroundColor: color_main_half,
          appBar: CommAppBar(
          backgroundColor: color_transparent,
          title: Text(
          '首页',
          ),
          ),
          body: Container(),
        );
      });
    }
}
2、初始化子页面
    pages = [
      IndexPage(),
      MyPage(),
    ];
 */
Widget PageTabBuild<T extends GetxController>({
  Key? key,bool? isAutomaticKeepAlive,
  required T initController,
  required String tag,
  required Function(T) builder
}){
  return _PageFullWidget(
    isAutomaticKeepAlive: isAutomaticKeepAlive,
    child: GetBuilder(
        init: Get.put(initController,tag: tag),
        tag: tag,
        builder: (T controller)=>builder(controller)
    ),
  );
}

/**
 * 用于new页面和 GetWidget 一起使用，不需要注册路由
例如：
    switch(tag){
      case '1':
        rightUi = TestPage1();
      break;
      case '2':
        rightUi = TestPage2();
      break;
    }
 */
Widget PageNewBuild<T extends GetxController>({
    Key? key,
    bool? isAutomaticKeepAlive,
    required T initController,
    required Function(T) builder}){
  return GetBuilder(
      // init: initController,
      init: Get.put(initController),
      global: isAutomaticKeepAlive??false,
      builder: (T controller)=>builder(controller)
  );
}
